<?php
include_once(_CORE_PATH. "mapper_prototype.php");
class PaginatorMapper extends mapper_prototype
{
    private $perPage;
    private $totalCount;
    private $page;
    private $totalPages;
    
    public function __construct()
    {
    	parent::__construct('paginator');
    }
    
	static public function GetInstance()
	{
		return new self();		
	}
    
    public function Set($totalCount, $currentPage = 1, $perPage = 30)
    {
        $this->perPage = $perPage > 0 ? $perPage : 30;
        $this->totalCount = $totalCount;
        $this->SetTotalPages();
        $this->SetPage($currentPage);
        return $this;
    }
    public function SetTotalCount ($totalCount = 0)
    {
        $this->totalCount = $totalCount;
        $this->SetTotalPages();
        //$this->SetPage($this->page);
        return $this;
    }
    public function SetPage($currentPage = 1)
    {
        /*if ($currentPage > $this->totalPages) {
            $this->page = $this->totalPages;
        } else*/
        if ($currentPage < 1) {
            $this->page = 1;
        } else {
            $this->page = $currentPage;
        }
        return $this;
    }
    public function SetPerPage ($perPage)
    {
        $this->perPage = $perPage > 0 ? $perPage : 30;
        $this->SetTotalPages();
        //$this->SetPage($this->page);
        return $this;
    }
    private function SetTotalPages ()
    {
        $this->totalPages = max(1, ceil($this->totalCount / $this->perPage));
        return $this;
    }
    public function GetSqlLimit ()
    {
        return (($this->page - 1) * $this->perPage) . ", " . $this->perPage;
    }
    public function GetPage ()
    {
        return $this->page;
    }
    public function GetTotalCount ()
    {
        return $this->totalPages;
    }
    public function GetPerPage ()
    {
        return $this->perPage;
    }
    public function GetTotalPages ()
    {
        return $this->totalPages;
    }
}